from pynput.keyboard import Key, Controller  # 导入按键控制
import time  # 导入时间包，调用sleep函数让琴键有时间间隔
import threading  # 导入线程包
from pywinauto import Desktop


# 演奏函数
def play_piano_quickly(music):
    for n in music:
        if n.isupper():
            keyboard.press(Key.shift)
            time.sleep(0.001)
            keyboard.press(n.lower())
            time.sleep(0.29)
            keyboard.release(n.lower())
            keyboard.release(Key.shift)
        elif n == "|" or n == ")":
            pass
        elif n in "!@$%^*(":
            keyboard.press(Key.shift)
            time.sleep(0.001)
            keyboard.press("1245689"["!@$%^*(".index(n)])
            time.sleep(0.29)
            keyboard.release("1245689"["!@$%^*(".index(n)])
            keyboard.release(Key.shift)
        elif n != " " and n != "-":
            keyboard.press(n)
            if music.index(n) != len(music) - 1 and music[music.index(n) + 1] == ")":
                time.sleep(0.15)
            else:
                time.sleep(0.3)
            keyboard.release(n)
        elif n == "-":
            time.sleep(0.6)
        else:
            time.sleep(0.3)


# 演奏函数
def play_piano_fast(music):
    for n in music:
        if n.isupper():
            keyboard.press(Key.shift)
            time.sleep(0.001)
            keyboard.press(n.lower())
            time.sleep(0.319)
            keyboard.release(n.lower())
            keyboard.release(Key.shift)
        elif n == "|" or n == ")":
            pass
        elif n in "!@$%^*(":
            keyboard.press(Key.shift)
            time.sleep(0.001)
            keyboard.press("1245689"["!@$%^*(".index(n)])
            time.sleep(0.319)
            keyboard.release("1245689"["!@$%^*(".index(n)])
            keyboard.release(Key.shift)
        elif n != " " and n != "-":
            keyboard.press(n)
            if music.index(n) != len(music) - 1 and music[music.index(n) + 1] == ")":
                time.sleep(0.16)
            else:
                time.sleep(0.32)
            keyboard.release(n)
        elif n == "-":
            time.sleep(0.32)
        else:
            time.sleep(0.16)


# 演奏函数
def play_piano_slowly(music):
    for n in music:
        if n.isupper():
            keyboard.press(Key.shift)
            time.sleep(0.001)
            keyboard.press(n.lower())
            time.sleep(0.479)
            keyboard.release(n.lower())
            keyboard.release(Key.shift)
        elif n == "|" or n == ")":
            pass
        elif n in "!@$%^*(":
            keyboard.press(Key.shift)
            time.sleep(0.001)
            keyboard.press("1245689"["!@$%^*(".index(n)])
            time.sleep(0.479)
            keyboard.release("1245689"["!@$%^*(".index(n)])
            keyboard.release(Key.shift)
        elif n != " " and n != "-":
            keyboard.press(n)
            if music.index(n) != len(music) - 1 and music[music.index(n) + 1] == ")":
                time.sleep(0.24)
            else:
                time.sleep(0.48)
            keyboard.release(n)
        elif n == "-":
            time.sleep(0.96)
        else:
            time.sleep(0.48)


def thread_play(play_piano, right):
    # 运行线程
    rt = threading.Thread(target=play_piano, args=(right,))  # 主旋律线程
    rt.start()
    rt.join()


if __name__ == '__main__':
    # 准备演奏
    # 控制键盘
    keyboard = Controller()
    # 切换到vue键盘钢琴(auto piano)网页
    keyboard.press(Key.cmd)
    time.sleep(1)
    keyboard.press("d")
    keyboard.release("d")
    keyboard.release(Key.cmd)
    # 链接的方式点击桌面任务栏的正在运行程序print_control_identifiers()
    dlg = Desktop(backend="uia").任务栏.运行中的程序.child_window(title="Google Chrome - 1 个运行窗口", auto_id="Chrome",
                                                         control_type="Button").click()
    time.sleep(2)
    # 右手
    right1 = "uopp-op|dd)f)dsp sp|y y y ty|puuyu uo|p p p op|dd)f)dsd hf|dd)f)dsdspo|pspopouo|p ---|uo|"
    right2 = "ppffd)f)d)s)ap)o)|pppsaOo |ppffd)f)d)s)ap)o)|pppf)d)d fd|"
    right3 = "pppa)p)opdd)f)dsp spyyyu)y)tyouuyu uo|pppa)p)opdd)f)dsd hfdd)f)dsdspopspopouop---|"
    right4 = "ppffd)f)d)s)ap)o)|pppsaOo |ppffd)f)d)s)ap)o)|pppsdaa |ppp aa)p)a |ssspdaas)a)|" \
             "ppp aa)p)a |sssphff |jj)f)f kk)f)f |ddds)d)fff|jj)f)f kk)f)f |ddds)d)ffo)p)|p---|"
    thread_play(play_piano_quickly, right1)
    thread_play(play_piano_fast, right3)
    thread_play(play_piano_slowly, right2)
    # thread_play(play_piano_fast, right3)
    thread_play(play_piano_slowly, right4)
